﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fintech.Utils;

namespace ZOVNP.WinView
{
    class MonthlyOrdersTreePopulationStrategy : IOrdersTreePopulationStrategy
    {
        private readonly Guid _id;
        private readonly string _text;

        public MonthlyOrdersTreePopulationStrategy()
        {
            _id = new Guid("08EDA786-3EB5-4195-A406-88BBFB4B1D6D");
            _text = "По месяцам.";
        }

        public Guid ID { get { return _id; } }
        public string Text { get { return _text; } }

        public void PopulateTree(TreeView treeView, IEnumerable<OrderNode> nodes)
        {
            var monthGroups = nodes.GroupBy(n => new Tuple<int, int>(n.Order.CreationDate.Year, n.Order.CreationDate.Month));
            monthGroups.ForEach(mg =>
                                    {
                                        var node = new TreeNode(mg.Key.Item1 + "." + mg.Key.Item2);
                                        mg.ForEach(n => node.Nodes.Add(n));
                                        treeView.Nodes.Add(node);
                                    });
        }

        public override string ToString()
        {
            return _text;
        }
    }
}
